﻿using HBase;
using hconnect;
using hCommand;
using Persistence;
using System;
using System.Collections.Generic;

namespace Controller
{
    public  class ReOrderTask : ZtTask {
        ICoreStockServer stockServer = WMSDalFactray.getDal<ICoreStockServer>();
        ICoreTaskServer taskServer = WMSDalFactray.getDal<ICoreTaskServer>();
        ICoreWhLocServer whLocServer = WMSDalFactray.getDal<ICoreWhLocServer>();
        public override bool excute() {
            int d = 0;
            for (int i = 0; i < int.MaxValue; i++) {

                d = i;
            }
            consoleOnly(InfoC.consoleOnly_reOrder, "begin sleep" +d);
      
            if (taskServer.countCoreTask(CoreTaskStatus.执行中)>0) {
                consoleOnly(InfoC.consoleOnly_reOrder, "任务进行中");
                return true;
            }
            
            List<CoreStock> list = stockServer.getCoreStockByStatus(StockStatus.库存);
            CoreStock cs = list[0];
            for (int i = 1; i < list.Count; i++) {
                if (cs.changeTime > list[i].createTime) {
                    cs = list[i];
                }
            }
            List<CoreWhLoc> l=    whLocServer.GetLocByWhAreaId(0);
            if (l.Count == 0) {
                consoleOnly(InfoC.consoleOnly_reOrder, "无有效货位");
                return true;
            }
            CoreWhLoc loc = null;
            int time = 0;

            do {
                time++;
                long index = DateTime.Now.Ticks % l.Count;
                loc = l[(int)index];
                if (time > 100) {
                    consoleOnly(InfoC.consoleOnly_reOrder, "获取目标货位失败");
                }
            } while (loc.countStock != 0);

            WMSTransactionFacade.doCraneMoveEngine(cs.id, loc.id);

            return true;
        }
    }
}